package com.mjl.shops.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    //字符换拼接
    public static String strJoining(String ... str){
        StringBuilder stringBuilder = new StringBuilder();
        for (String s:str){
            stringBuilder.append(s);
        }
        return stringBuilder.toString();
    }

    //字符串是否为空
    public static boolean strEmpty(String str){
        if (str!=null&&!str.isEmpty()&&str.length()>0&&!str.equals("")){
            return true;
        }else{
            return false;
        }
    }

    //字符串是否为手机号
    public static boolean isPhone(String phone){
        String s = "^((13[0-9])|(15[^4,\\D])|(16[67])|(18[0-9])|(14[579])|(17[01235678])|(19[0126789]))\\d{8}$";
        Pattern p = Pattern.compile(s);
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    //md5加密字符串
    public static String setMD5(String str){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] encryContext = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < encryContext.length; offset++) {
                i = encryContext[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
